﻿
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIBattleChat : UIObject
{
    public InputField inputFieldChat;
    public Text textChatPrefab;

 
    public void OnButtonChatClick(Button button)
    {
        var chatContent = inputFieldChat.text.Trim();
        if (string.IsNullOrEmpty(chatContent))
        {
            UITips.Instance.Show(XLocalization.Get("请输入聊天内容"));
            return;
        }
        var req = new chat();
        req.msg = chatContent;
        WebSocketClient.Instance.SendSerialize(button, req);
    }

    public void OnButtonChatPrefabClick(Button button)
    {
        //var chatContent = textChatPrefab.text.Trim();
        //if (string.IsNullOrEmpty(chatContent))
        //    return;
        //var req = new actionbattle();
        //req.type = ActionType.Chat;
        //req.touid = "";//temp
        //req.content = chatContent;
        //WebSocketClient.Instance.SendSerialize(req);

        WebSocketClient.Instance.SendMsgName(button, nameof(dealcard));
    }
    private void OnHandleActionbattleRsp(System.Object data)
    {
        var rsp = data as actionbattle_rsp;
        if (rsp.ret != NetErrorCode.None)
        {
            UITips.Instance.Show("出错了:" + rsp.ret + " " + rsp.reason);
        }
    }

    private void OnHandleDealcardRsp(System.Object data)
    {
        var rsp = data as dealcard_rsp;
        if (rsp.ret != NetErrorCode.None)
        {
            UITips.Instance.Show("出错了 code:" + rsp.ret + "=>" + rsp.reason);
        }
    }

    private void OnHandleChatRsp(System.Object data)
    {
        var rsp = data as chat_rsp;
        if (rsp.ret != NetErrorCode.None)
        {
            UITips.Instance.Show("出错了 code:" + rsp.ret + "=>" + rsp.reason);
        }
    }

    private DataBattleMgr _dataBattleMgr;
    public void Bind(DataBattleMgr data)
    {
        _dataBattleMgr = data;
        foreach (var player in _dataBattleMgr.userid2data.Values)
        {
            player.onCardTypeChange += OnCardTypeChange;

           // if (player.GetUserId() != DataPlayer.Instance.userId)
           //     player.OnPlayerChallenge += OnPlayerChallenge;
        }  
    }

    public void Unbind()
    {
        foreach (var player in _dataBattleMgr.userid2data.Values)
        {
            player.onCardTypeChange -= OnCardTypeChange;

            //if (player.GetUserId() != DataPlayer.Instance.userId)
            //    player.OnPlayerChallenge -= OnPlayerChallenge;
        }
        _dataBattleMgr = null;
    }

    private void OnCardTypeChange(CardType state)
    {

    }

    private void OnPlayerChallenge(string challenge)
    {
        //if(Random.Range(0.0f, 1.0f) > 0.5)
            //textChatPrefab.text = challenge + "?" + XLocalization.Get("你疯了吧？");
        //else
        //    textChatPrefab.text = XLocalization.Get("还没轮到你喊，不要着急");
    }

    protected override void onVisible()
    {
        base.onVisible();
        //textChatPrefab.text = XLocalization.Get("先摇塞子,我先喊!逆时针顺序喊");
        Facade.Instance.RegistNetEvent(nameof(actionbattle_rsp), OnHandleActionbattleRsp);
        Facade.Instance.RegistNetEvent(nameof(dealcard_rsp), OnHandleDealcardRsp);
        Facade.Instance.RegistNetEvent(nameof(chat_rsp), OnHandleChatRsp);
    }
    protected override void OnInvisible()
    {
        base.OnInvisible();
        Facade.Instance.UnRegistNetEvent(nameof(actionbattle_rsp), OnHandleActionbattleRsp);
        Facade.Instance.UnRegistNetEvent(nameof(dealcard_rsp), OnHandleDealcardRsp);
        Facade.Instance.UnRegistNetEvent(nameof(chat_rsp), OnHandleChatRsp);
    }
}